package mutsumimod.ui;

import basemod.CustomCharacterSelectScreen;
import basemod.ReflectionHacks;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.evacipated.cardcrawl.modthespire.lib.*;
import com.evacipated.cardcrawl.modthespire.patcher.PatchingException;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.helpers.input.InputHelper;
import com.megacrit.cardcrawl.screens.charSelect.CharacterOption;
import javassist.CannotCompileException;
import javassist.CtBehavior;
import mutsumimod.character.Mutsumi;

import java.util.ArrayList;

import static mutsumimod.MutsumiMod.imagePath;


public class ChangeIcon extends IconUi {

    public static Texture icon;
    public static boolean isMortis=false;

    static {
        icon = ImageMaster.loadImage(imagePath("ui/change.png"));
    }


    public ChangeIcon() {
        super(icon,100.0f,500.0f);

    }

    @Override
    public void render(SpriteBatch sb) {
        super.render(sb);
    }

    @Override
    public void update() {
        super.update();
        updateInput();
    }

    public void updateInput() {
        hb.update();
        if (InputHelper.justClickedLeft) {
            if (this.hb.hovered) {
                this.hb.clickStarted = true;
            }
        }
        if(hb.clicked){
            changeOptionIcon();
            hb.clicked = false;
        }
    }


    public void changeOptionIcon() {
        CardCrawlGame.sound.playA("UI_CLICK_1", -0.4F);
        Object charSelectScreen = CardCrawlGame.mainMenuScreen.charSelectScreen;
        ArrayList<CharacterOption> allOptions;
        allOptions = ReflectionHacks.getPrivate(charSelectScreen, CustomCharacterSelectScreen.class, "allOptions");
        for(CharacterOption characterOption : allOptions){
            if(characterOption.c instanceof Mutsumi){
                characterOption.c.doCharSelectScreenSelectEffect();
                if(!isMortis){
                    isMortis=true;
                    setMortis(characterOption);
                }else{
                    isMortis=false;
                    setMutsumi(characterOption);
                }
            }
        }
    }

    public static void setMortis(CharacterOption characterOption){
        ReflectionHacks.setPrivate(characterOption,CharacterOption.class,"buttonImg",ImageMaster.loadImage(imagePath("character/select/button_mortis.png")));
        CardCrawlGame.mainMenuScreen.charSelectScreen.bgCharImg=ImageMaster.loadImage(imagePath("character/select/mortis_bg.png"));
        characterOption.name="墨提斯";
    }

    public static void setMutsumi(CharacterOption characterOption){
        ReflectionHacks.setPrivate(characterOption,CharacterOption.class,"buttonImg",ImageMaster.loadImage(imagePath("character/select/button_mutsumi.png")));
        CardCrawlGame.mainMenuScreen.charSelectScreen.bgCharImg=ImageMaster.loadImage(imagePath("character/select/mutsumi_bg.png"));
        characterOption.name="若叶睦";
    }


    @SpirePatch(clz = CharacterOption.class, method = "updateHitbox")
    public static class resetMutsumi{
        @SpireInsertPatch(
                locator=Locator.class
        )
        public static void insert(CharacterOption __instance){
            if(__instance.c instanceof Mutsumi){
                if(ChangeIcon.isMortis){
                   setMortis(__instance);
                }else{
                    setMutsumi(__instance);
                }
            }
        }

        private static class Locator extends SpireInsertLocator {
            public int[] Locate(CtBehavior ctMethodToPatch) throws CannotCompileException, PatchingException {
                Matcher finalMatcher = new Matcher.MethodCallMatcher(AbstractPlayer.class, "getPrefs");
                int[] lines = LineFinder.findInOrder(ctMethodToPatch, new ArrayList<Matcher>(), finalMatcher);
                return new int[]{lines[0]};
            }
        }
    }
}